<template>
     <PPMPage
      v-if="pageData.length > 0"
      :formData="formData"
      :pageData="pageData"
    >
      <template slot="footer" class="dialog-footer">
       <el-row>
        <el-col :span="24" class="text-center">
          <el-button type="primary" size="medium" @click="submitFun">保存</el-button>
          <el-button size="medium" @click="closeFun">取消</el-button>
        </el-col>
      </el-row>
      </template>
    </PPMPage>
</template>
<script>
import ppmmixin from "@/mixins/ppmmixin.js";
import modifySave from "@/utils/modifySave";
import OrderService from "@/api/order/OrderService.js";
import CustOrderService from "@/api/order/CustOrderService";
import { mapState } from "vuex";
import requestUtil from '@/utils/requestEnc';
import custOrderDetail from '@/mixins/custorder/custOrderDetail';
export default {
 name: "ChangePPM",
  mixins: [ppmmixin,custOrderDetail],
  data() {
    return {
      result: [],
      formLabelWidth: "120px",
      fullscreenLoading: false,
      ppmParams:{},
      newCTOInfo:[],
      orderTask:{}
    };
  },
  computed: {
    ...mapState("orders", ["orderPosInfo"]),
    ...mapState("user", ["user", "busiManagerId", "custManagerId"]),
  },
   props:{
	  	ODKInfo: {
			type: Object,
			default: () => {
				return {};
			},
		},
		CTOInfo: {
			type: Array,
			default: () => {
				return [];
			},
		},
  },
  async mounted(){
     var custOrder =  JSON.parse(localStorage.getItem("custOrder_select"));
     
     
     let ppmCondition = {
          offerId:this.CTOInfo[0].prodOfferId,
          productId: '80008007',
          prodName:this.CTOInfo[0].offerName,
          serviceOfferId: "-1", //新装所有产品固定值
          modType: "50200003", //新装所有产品固定值
          optionId: "STACHE00270001", //新装所有产品固定值
        };
        sessionStorage.setItem("ppmCondition", JSON.stringify(ppmCondition));

      //得到属性
    //   this.ppmParams=JSON.parse(sessionStorage.getItem('ppmCondition'));
      
      //设置属性
      this.loadPPMConfig(ppmCondition);


    if(this.CTOInfo instanceof Array){
      this.newCTOInfo = this.CTOInfo;
    }else{
      this.newCTOInfo = [this.CTOInfo];
    }
    
    if(this.newCTOInfo.length > 0){
      var orderItemIds="";

      for(var i=0;i<this.newCTOInfo.length;i++){
         if(i==0){
           orderItemIds += this.newCTOInfo[i].orderItemId;
         }else{
            orderItemIds += ","+this.newCTOInfo[i].orderItemId;
         }
      }

      var custSoNumber = this.newCTOInfo[0].custSoNumber;
      var batchNbr =  this.newCTOInfo[0].batchNbr;
      var custId = this.newCTOInfo[0].custId;
      var taskId = this.newCTOInfo[0].taskId;


      this.orderItemIds = orderItemIds;


    let usersMsg = {
        acceptUserId: this.user.staffId,
        acceptUserOrg: this.user.orgId,
        systemUserId: this.user.sysUserId,
        dealType: 234,
        orderItemId:this.newCTOInfo[0].orderItemId,
        orderAuditId:this.newCTOInfo[0].orderAuditId
      };
      // 工单级别dealType:1 销售品级别dealType:234
      const result = await CustOrderService.queryOrderItemDealTask(usersMsg);

      

      var  orderTasks = result.meta.orderItemDealTasks;
      // var orderTasks = this.ODKInfo;

      for(var i=0;i<orderTasks.length;i++){
        //全程报竣
        if(orderTasks[i].taskId == 'STACHE00270001'){
          var orderTask = orderTasks[i];
          break;
        }
      }


      this.orderTask = orderTask;
      

      var queryCTOInfo = this.newCTOInfo[0];
      this.ODKInfo.taskId = "STACHE00270001";
      await this.getCustomerOrderDetail(this.formData,queryCTOInfo,this.ODKInfo);
      
      this.formData["12011-141000049"].value = "全程报竣";
      this.formData["12011-100010135"].value = this.newCTOInfo[0].custSoNumber;
      this.formData["12011-141000050"].value = this.newCTOInfo[0].prodName;
      this.formData["12011-100012440"].value = this.$util.dateFormat(new Date(),"yyyy-MM-dd");
      this.formData["12011-100012377"].value = this.user.staffName;


    }

  },
  methods: {
    async submitFun() {
      let base = {
        statusCds: this.newCTOInfo[0].statusCd, //订单状态
        custSoNumber: this.newCTOInfo[0].custSoNumber, //流水号  从上个页面带回
        handleOrgId: this.user.orgId,
        custId: this.newCTOInfo[0].custId,
        batchNbr: this.newCTOInfo[0].batchNbr, //批次号
        accNum: '', //设备号,没有不传
        orderItemIds: this.orderItemIds,
        taskId: this.orderTask.taskId,
        orderAuditId: this.newCTOInfo[0].orderAuditId,
        oneOrmany: 'one',
        orderTaskId: this.orderTask.orderTaskId,
        userId: this.user.staffId,
        orderItemId: this.orderItemIds,
        processInstId: this.orderTask.processInstId,
        activityId: this.orderTask.activityInstId,

      };
      
      let param = requestUtil.paramEnc(this.formData, base);
      
      const result = await CustOrderService.saveCompletion({...param, ...base});
      try {
        if (result.meta.resultCode == 0) {
          this.$message({
            type: 'success',
            message: '保存成功!',
          });
          this.$emit('update:show', false);
        } else {
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: 'error',
          });
        }
      } catch {
        this.$message({
          showClose: true,
          message: 'code:' + result.code + '  msg:' + result.msg,
          type: 'error',
        });
      }
    }, closeFun() {
      this.$emit('close');
    },
  },
}
</script>
<style scoped>

</style>


